<template>
    <div>
        <ContentHead title="创建服务器" content="服务器管理与权限管理" ref="contentHead"></ContentHead>
        <div class="server-content">
            <div style="width: 830px">
                <div class="head-project">
                    <div class="title"><span>服务器信息</span></div>
                </div>
                <Form ref="serverForm" :model="server" label-position="top" :rules="ruleValidate">
                    <Row :gutter="16">
                        <Col span="12">
                            <FormItem label="服务器名称" prop="hostName">
                                <Input v-model="server.hostName" placeholder="e.g. 10.0.0.10 or mydocker.domain.com"></Input>
                            </FormItem>
                        </Col>
                        <Col span="12">
                            <FormItem label="服务器地址" prop="endpointUrl">
                                <div class="item-line-right">
                                    <Input v-model="server.endpointUrl" placeholder="e.g. 10.0.0.10 or mydocker.domain.com"></Input>
                                    <Input v-model="server.endpointPort" style="width: 120px" placeholder="e.g. 2376"></Input>
                                </div>
                            </FormItem>
                        </Col>
                    </Row>
                    <FormItem label="所属成员" v-if="isGit">
                        <Select v-model="users" filterable multiple remote :remote-method="searchUser">
                            <Option v-for="item in userList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                        </Select>
                    </FormItem>
                    <FormItem label="所属团队" v-if="isGit">
                        <Select v-model="groups" filterable multiple  remote :remote-method="searchGroup">
                            <Option v-for="item in groupList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                        </Select>
                    </FormItem>
                    <FormItem label="TLS">
                        <i-switch v-model="server.isTls" size="large" true-value="1" false-value="0">
                            <span slot="open">On</span>
                            <span slot="close">Off</span>
                        </i-switch>
                    </FormItem>
                    <div v-if="server.isTls === '1'">
                        <FormItem label="CA certificate" prop="tlsCaFile">
                            <FileUpload v-model="server.tlsCaFile"></FileUpload>
                        </FormItem>
                        <FormItem label="certificate" prop="tlsCertFile">
                            <FileUpload v-model="server.tlsCertFile"></FileUpload>
                        </FormItem>
                        <FormItem label="key" prop="tlsKeyFile">
                            <FileUpload v-model="server.tlsKeyFile"></FileUpload>
                        </FormItem>
                    </div>
                </Form>
                <div style="text-align: center">
                    <Button type="primary" @click="save">save</Button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import FileUpload from '../../components/FileUpload'
    export default {
        data(){
            return {
                server:{
                    hostName:'',
                    loginName: '',
                    password: '',
                    endpointUrl: '',
                    endpointPort: '',
                    isTls: '1',
                    tlsCaFile: null,
                    tlsCertFile: null,
                    tlsKeyFile: null,
                    envTag: 'test'
                },
                groups: [],
                users: [],
                userList: [],
                groupList: [],
                ruleValidate:{
                    hostName: [{
                        required: true, message: '请为你的服务器命名', trigger: 'blur'
                    }],
                    endpointUrl: [{
                        required: true, message: '服务器地址不能为空', trigger: 'blur'
                    }],
                    endpointPort: [{
                        required: true, message: '服务器端口不能为空', trigger: 'blur'
                    }],
                    tlsCaFile:[{
                        required: true, message: '请上传CA认证文件', trigger: 'change'
                    }],
                    tlsCertFile:[{
                        required: true, message: '请上传认证文件', trigger: 'change'
                    }],
                    tlsKeyFile:[{
                        required: true, message: '请上传秘钥文件', trigger: 'change'
                    }]
                },
                isGit: true
            }
        },
        created(){
            this.searchUser('');
            this.searchGroup('');
        },
        components:{
            FileUpload
        },
        methods:{
            searchUser(query){
                this.$axios.get('/mdo/api/v1/gitLab/user', {
                    params: {
                        search: query
                    }
                }).then(res => {
                    this.userList = res.data;
                }).catch(e => {
                    this.isGit = false;
                });
            },
            searchGroup(query){
                this.$axios.get('/mdo/api/v1/gitLab/group', {
                    params: {
                        search: query
                    }
                }).then(res => {
                    this.groupList = res.data;
                }).catch(e => {
                    this.isGit = false;
                });
            },
            save() {
                this.$refs['serverForm'].validate(valid => {
                    if (valid) {
                        this.$axios.post('/mdo/api/v1/machine/save', {
                            server: this.server,
                            users: this.users,
                            groups: this.groups
                        }).then(res=>{
                            this.$Message.success('success');
                            this.$router.push({path: '/server'})
                        }).catch(e => {
                            this.$Message.error(e.response.data);
                        });
                    }
                })
            }
        }
    }
</script>

<style lang="less" scoped>
    .server-content{
        width: 1024px;
        margin: 0 auto;
        padding: 16px 0;
        display: flex;

        .head-project {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .title {
                width: 120px;
                padding: 6px 0;
                border-bottom: 1px solid @primary-color;
                span {
                    font-size: 18px;
                    font-weight: bold;
                }
            }
            border-bottom: 1px solid @border-color-base;
            margin-bottom: 16px;
        }
    }
</style>

<style lang="less">
    .server-content{
        .item-line-right{
            display: flex;
            align-items: center;

            &>div:first-child{
                input{
                    border-top-right-radius: 0;
                    border-bottom-right-radius: 0;
                }
            }

            &>div:last-child{
                input{
                    border-left: none;
                    border-top-left-radius: 0;
                    border-bottom-left-radius: 0;
                }
            }
        }
    }
</style>